package com.hourse.platform.utils;

import java.util.Set;

/**
 * @使用描述: FlowTools
 */
public class FlowTools {


  static String roleItemSql(){

    Set<String> roleCodes = CurrentThread.getRoles();
    Object[] roleArray =  roleCodes.toArray();

    String roleInSql = "";

    if(roleArray.length > 0){
      for(int i = 0; i < roleArray.length ; i++){
        roleInSql += "'"+roleArray[i]+"'";
        if(i != roleArray.length - 1){
          roleInSql += ',';
        }
      }
    } else {
      roleInSql = "'_NULL'";
    }
    return  roleInSql;
  }

  public static String SQL(){

    String roleItemSql = roleItemSql();

    SqlParams sqlParams = SqlParams.New();
    sqlParams.append(" SELECT ");
    sqlParams.append(" _flow_instance.id	as _id,");
    sqlParams.append(" _flow_node.name  as _name,");
    sqlParams.append(" _flow_instance.status as _status,");
    sqlParams.append(" _flow_node.role_code as _role_code,");
    sqlParams.append(" if(_flow_node.role_code IN (");
    sqlParams.append(roleItemSql);
    sqlParams.append(" ),'Y','N')");
    sqlParams.append(" FROM");
    sqlParams.append(" flow_instance _flow_instance");
    sqlParams.append(" LEFT JOIN flow_node _flow_node ON _flow_instance.current_node_id = _flow_node.id");
    sqlParams.append(" WHERE");
    sqlParams.append(" _flow_node.role_code IN (");
    sqlParams.append(roleItemSql);
    sqlParams.append(" ) OR EXISTS (");
    sqlParams.append("  SELECT");
    sqlParams.append(" 1");
    sqlParams.append(" FROM");
    sqlParams.append(" flow_process _flow_process");
    sqlParams.append(" WHERE");
    sqlParams.append(" _flow_process.create_user_id = '"+ AppTools.GetCurrentUserId() +"'");
    sqlParams.append(" AND _flow_process.instance_id = _flow_instance.id");
    sqlParams.append(" )");

    return sqlParams.getSql();
  }

  public static String HQL(){

    String roleItemSql = roleItemSql();


    SqlParams sqlParams = SqlParams.New();
    sqlParams.append(" SELECT ");
    sqlParams.append(" _flow_instance.id	as _id,");
    sqlParams.append(" _flow_node.name  as _name,");
    sqlParams.append(" _flow_instance.status as _status,");
    sqlParams.append(" _flow_node.roleCode as _role_code");
//    sqlParams.append(" , if(_flow_node.roleCode IN (");
//    sqlParams.append(roleItemSql);
//    sqlParams.append(" ),'Y','N')");
    sqlParams.append(" from");
    sqlParams.append(" FlowInstanceEntity _flow_instance");
    sqlParams.append(" LEFT JOIN FlowNodeEntity _flow_node ON _flow_instance.currentNodeId = _flow_node.id");
    sqlParams.append(" WHERE");
    sqlParams.append(" _flow_node.roleCode IN (");
    sqlParams.append(roleItemSql);
    sqlParams.append(" ) OR EXISTS(");
    sqlParams.append("  SELECT");
    sqlParams.append(" 1");
    sqlParams.append(" from");
    sqlParams.append(" FlowProcessEntity _flow_process");
    sqlParams.append(" WHERE");
    sqlParams.append(" _flow_process.createUserId = '"+ AppTools.GetCurrentUserId() +"'");
    sqlParams.append(" AND _flow_process.instanceId = _flow_instance.id");
    sqlParams.append(" )");

    return sqlParams.getSql();
  }
}
